<template>
  <el-col :span="12" :offset="6">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="auto"
    >
      <el-form-item label="数据同步时间">
        <el-time-picker placeholder="选择时间" v-model="dataForm.date1" style="width: 100%;"></el-time-picker>
      </el-form-item>
      <el-form-item label="人脸立柱重启时间">
        <el-time-picker placeholder="选择时间" v-model="dataForm.date2" style="width: 100%;"></el-time-picker>
      </el-form-item>
      <el-form-item label="车辆闸机重启时间">
        <el-time-picker placeholder="选择时间" v-model="dataForm.date3" style="width: 100%;"></el-time-picker>
      </el-form-item>
      <el-form-item label="数据重发次数">
        <el-input-number
          v-model="dataForm.num"
          @change="handleChange"
          :min="1"
          :max="20"
          label="描述文字"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <el-col :span="12" :offset="6">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </el-col>
  </el-col>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    var validatedate1 = (rule, value, callback) => {
      if (!this.dataForm.date1 && !/\S/.test(value)) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    var validatedate2 = (rule, value, callback) => {
      if (!this.dataForm.date2 && !/\S/.test(value)) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    var validatedate3 = (rule, value, callback) => {
      if (!this.dataForm.date3 && !/\S/.test(value)) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };

    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        date1: "",
        date2: "",
        date3: "",
        num: 1,

        status: 1
      },
      dataRule: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        date1: [{ validator: validatedate1, trigger: "blur" }],

        date2: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { validator: validatedate2, trigger: "blur" }
        ],
        date3: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { validator: validatedate3, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handleChange() {},
    init(id) {
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "post",
        params: this.$http.adornParams()
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
              method: "POST",
              params: this.$http.adornParams()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.username;
                this.dataForm.salt = data.user.salt;
                this.dataForm.email = data.user.email;
                this.dataForm.mobile = data.user.mobile;
                this.dataForm.roleIdList = data.user.roleIdList;
                this.dataForm.status = data.user.status;
              }
            });
          }
        });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/user/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.id || undefined,
              username: this.dataForm.userName,
              password: this.dataForm.password,
              salt: this.dataForm.salt,
              email: this.dataForm.email,
              mobile: this.dataForm.mobile,
              status: this.dataForm.status,
              roleIdList: this.dataForm.roleIdList
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
